﻿Imports System.Collections.Generic
Imports System.Linq

Partial Class bo_index
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        txtUserName.Focus()
        Dim ua As New OneCall.BO.UserAuth(Request, Response)
        ua.SignOut(Request)
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim flag As Boolean = False
        If Trim(txtUserName.Text) = "" Then
            lblErrUsername.Visible = True
            flag = True
        Else
            lblErrUsername.Visible = False
        End If
        If Trim(txtPassword.Text) = "" Then
            lblErrPassword.Visible = True
            flag = True
        Else
            lblErrPassword.Visible = False
        End If
        If (flag) Then Exit Sub

        Dim SQLDB As New OneCall.DataModel.OneCDataEntities

        Dim userlist As List(Of OneCall.DataModel.User) = (From p In SQLDB.Users Where Trim(p.User_UserName) = Trim(txtUserName.Text) Select p).ToList()

        If userlist.Count <= 0 Then
            lblErrUsername.Visible = True
            SQLDB.Dispose()
            SQLDB = Nothing
            Exit Sub
        Else
            If userlist(0).User_Password = txtPassword.Text Then
                '//done
                Dim ua As New OneCall.BO.UserAuth(Request, Response)
                ua.RegisterUserSession(Response, userlist(0).User_ID, False)
                '////
                SQLDB.Dispose()
                SQLDB = Nothing
            Else
                lblErrPassword.Visible = True
                SQLDB.Dispose()
                SQLDB = Nothing
                Exit Sub
            End If
        End If
    End Sub
End Class
